package com.formula.datastructure.question.tree;

// Page141, 6-1
// (a-(b+c))*(d/e)
public class Quest03 {
    public static void main(String[] args) {
        Node03 root = new Node03("*");
        Node03 a1 = new Node03("-");
        Node03 a2 = new Node03("/");
        Node03 b1 = new Node03("a");
        Node03 b2 = new Node03("b");
        Node03 b3 = new Node03("c");
        Node03 b4 = new Node03("d");

        root.leftChild = a1;
        root.rightChild = a2;

        a1.leftChild = b1;
        a1.rightChild = b2;
        a2.leftChild = b3;
        a2.rightChild = b4;

        Tree03 tree = new Tree03(root);
        tree.result();
    }
}

class Tree03 {
    private Node03 root;

    public Tree03(Node03 node) {
        root = node;
    }

    public void result() {
        if (root != null) {
            System.out.println(root.result());
        }
    }
}

class Node03 {
    public String data;
    public Node03 leftChild;
    public Node03 rightChild;

    public Node03(String data) {
        this.data = data;
    }

    public String result() {
        if (leftChild == null) {
            return data;
        } else {
            return compute();
        }
    }

    public String compute() {
        return "(" + leftChild.result() + data + rightChild.result() + ")";
    }
}


